//
//  DYSimilarPicMutiView.m
//  PrivatePhoto
//
//  Created by PrivatePhoto Team on 2021.
//  Copyright © 2021年 PrivatePhoto Team. All rights reserved.
//

#import "DYSimilarPicMutiView.h"
#import "VLocalImageCache.h"
#import "FPPhotosTool.h"
#import <ReactiveObjC.h>
#import "DYSimilarPicItemCell.h"
#import "DYSimilarPicModel.h"
@interface DYSimilarPicMutiView()<UICollectionViewDelegate, UICollectionViewDataSource>
{
    CGFloat _margin;
    CGFloat col;
}
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSMutableArray *assetArray;
@end

@implementation DYSimilarPicMutiView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        _margin = 2;
        col = 4;
        [self configSubView];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.frame = CGRectMake(0, 0, self.width, self.height);
}

- (void)configSubView{

    [self addSubview:self.collectionView];
   
}

- (void)handleCellForSelectedButtonNumberShowEvent:(NSIndexPath *)currentIndexPath
{
    BOOL isAdd = NO;
    NSInteger index = currentIndexPath.item;
    DYSimilarPicModel *model = self.imgArray[index];
    if ([self.seletedArray containsObject:model]) {
        [self.seletedArray removeObject:model];
        isAdd = NO;
    }else{
        [self.seletedArray addObject:model];
        isAdd = YES;
    }
    [self.collectionView reloadData];
    !self.SimilarPicMutiViewBlock ?:self.SimilarPicMutiViewBlock(model,isAdd,self.seletedArray);
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DYSimilarPicModel *model = self.imgArray[indexPath.item];
    !self.lookDetailBlock ?: self.lookDetailBlock(model,indexPath.item,self.assetArray);
//    [FPPhotosTool browserImagesWithAssetArray:self.assetArray index:indexPath.item];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((self.bounds.size.width - _margin * (col + 1)) / col, (self.bounds.size.width - _margin * (col + 1)) / col);
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    DYSimilarPicItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DYSimilarPicItemCell" forIndexPath:indexPath];
    DYSimilarPicModel *model = self.imgArray[indexPath.item];
    [cell updateViewWithModel:model selectedArray:self.seletedArray];
      __weak typeof(self) weakSelf = self;
    cell.selectPhotoCompleteBlock = ^{
          [weakSelf handleCellForSelectedButtonNumberShowEvent:indexPath];
    };
      return cell;
    
}

#pragma mark --- setter/getter

- (void)setImgArray:(NSArray *)imgArray{
    _imgArray = imgArray;
    if (self.assetArray.count) {
        [self.assetArray removeAllObjects];
    }
    @weakify(self)
    [imgArray enumerateObjectsUsingBlock:^(DYSimilarPicModel  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       @strongify(self)
        [self.assetArray addObject:obj.asset];
    }];
    [self.collectionView reloadData];
}

- (void)setSeletedArray:(NSMutableArray *)seletedArray{
    _seletedArray = seletedArray;
    [self.collectionView reloadData];
}


- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.allowsMultipleSelection = YES;
        _collectionView.bounces = YES;
        _collectionView.scrollEnabled = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[DYSimilarPicItemCell class] forCellWithReuseIdentifier:@"DYSimilarPicItemCell"];
       
    }
    return _collectionView;
}
- (UICollectionViewFlowLayout *)flowLayout {
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.itemSize = CGSizeMake(91, 91);
        [_flowLayout setSectionInset:UIEdgeInsetsMake(0, _margin, 0, _margin)];
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _flowLayout.minimumLineSpacing = _margin;
        _flowLayout.minimumInteritemSpacing = _margin;
    }
    return _flowLayout;
}

- (NSMutableArray *)assetArray{
    if (!_assetArray) {
        _assetArray = [[NSMutableArray alloc] init];
    }
    return _assetArray;
}

@end
